package com.study.quiz.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.study.quiz.dto.PageDTO;
import com.study.quiz.service.QuestionService;
import com.study.quiz.utils.ErrorIndexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CJunGod
 * @create 2020-03-03 18:59
 **/
@Slf4j
@Controller
public class IndexController {

    @Resource
    private QuestionService questionService;

    @Resource
    private ErrorIndexUtil errorIndexUtil;

    @GetMapping("/")
    public String hello(HttpServletRequest request, ModelMap modelMap,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "6") Integer size,
                        @RequestParam(value = "search", required = false) String search) {

        if (StringUtils.isNotEmpty(errorIndexUtil.getErrorMesg())) {
            modelMap.put("publishError", errorIndexUtil.getErrorMesg());
            errorIndexUtil.errorIndex(null);
        }

        PageDTO pageDto = questionService.findList(search, page, size);
        modelMap.put("pageDto", pageDto);
        modelMap.put("search", search);
        modelMap.put("page", pageDto.getPage());

        return "index";
    }

}
